﻿/*
 *  ChgDoc CMS - GNU General Public License, version 3 (GPL-3.0)
 *  Copyright (C)  2012. Ian Quigley
 *  
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *  
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using ChgDoc.Entity;
using System.Data;

namespace ChgDoc.Edit
{
    [AdminPage("Locations", "Admin")]
    public partial class LocationList : ChgDoc.Edit.BasePage
    {
        protected override IEnumerable<FormRule> GetFormRules()
        {
            return new List<FormRuleMapping<Location>>()
		                {
		                    new FormRuleMapping<Location>( txtCode, "Code", FormRuleCondition.NotNull, (s,v) => s.Code = (string)v),
                            new FormRuleMapping<Location>( txtName, "Name", FormRuleCondition.NotNull, (s,v) => s.Name = (string)v)                            
		                };
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
                return;

            UpdateDisplay();
        }

        private void UpdateDisplay()
        {
            grdList.DataSource = Location.ReadAll();
            grdList.DataBind();

            rptLocation.DataSource = Location.ReadAll();
            rptLocation.DataBind();
        }

        protected void rptLocation_ItemDataBound(object src, System.Web.UI.WebControls.RepeaterItemEventArgs e)
        {
            RepeaterItem item = e.Item;
            if( (item.ItemType == ListItemType.Item) || (item.ItemType == ListItemType.AlternatingItem) )
            {
                var rptLocationArticle = (Repeater)item.FindControl("rptLocationArticle");
                Location drv = (Location)item.DataItem;
                rptLocationArticle.DataSource = ArticleLocation.ArticlesFor(drv.Id);
                rptLocationArticle.DataBind();
            }

        }

        protected void grdList_Edit( object source, DataGridCommandEventArgs e )
        {
            grdList.EditItemIndex = e.Item.ItemIndex;
            UpdateDisplay();
        }

        protected void grdList_Cancel( object source, DataGridCommandEventArgs e )
        {
            grdList.EditItemIndex = -1;
            UpdateDisplay();
        }

        protected void grdList_Update( object source, DataGridCommandEventArgs e )
        {
            int id = int.Parse( e.Item.Cells[ 0 ].Text );
            string code = ((TextBox)e.Item.Cells[ 1 ].Controls[ 0 ]).Text;
            string name = ((TextBox)e.Item.Cells[ 2 ].Controls[ 0 ]).Text;

            if (!String.IsNullOrWhiteSpace( code ) && !String.IsNullOrWhiteSpace( name ))
                Location.Save( id, code, name );
            
            grdList.EditItemIndex = -1;
            UpdateDisplay();
        }

        protected void grdList_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            long id = long.Parse(e.Item.Cells[0].Text);

            switch (e.CommandName)
            {
                case "Delete":
                    Location.Delete( id );
                    UpdateDisplay();
                    break;

                case "Articles":
                    Response.Redirect( "LocationArticleList.aspx?locationId=" + id );
                    break;
            }
        }

        protected void btnAdd_click( object source, EventArgs e )
        {
            Location model = new Location() { Id = LongParam( "Id" ) };

            if (!PopulateModelFromControls<Location>( model ))
                return;

            model.Save();

            UpdateDisplay();
        }
    }
}